# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=p7zip
pkgver=17.03
pkgrel=1
pkgdesc='Command-line version of the 7zip compressed file archiver'
url="https://github.com/jinfeihan57/p7zip"
license=('LGPL' 'custom:unRAR')
arch=('i686' 'x86_64')
depends=('gcc-libs' 'bash')
makedepends=('nasm')
groups=('compression')
options=('emptydirs' '!makeflags')
source=("https://github.com/jinfeihan57/p7zip/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        03_respect-umask-partial_9.20.1.patch
        p7zip-9.20.1-1.src.patch)
sha256sums=('bb4b9b21584c0e076e0b4b2705af0dbe7ac19d378aa7f09a79da33a5b3293187'
            'd57c05631e84abd924fb13afc617673106373f53c274711e06ca7ff12982c11c'
            '2980374c6cc9369f73a96b2681e5e63f9f8b9e359ddaf6e09483db6cebe57b4f')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -p1 -i ${srcdir}/03_respect-umask-partial_9.20.1.patch
  patch -p1 -i ${srcdir}/p7zip-9.20.1-1.src.patch

  [[ $CARCH = x86_64 ]] \
    && cp makefile.cygwin64_asm makefile.machine \
    || cp makefile.cygwin_asm makefile.machine
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make all3 OPTFLAGS="${CXXFLAGS}"
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make test
  make test_7z
  make test_7zr
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make install \
    DEST_DIR="${pkgdir}" \
    DEST_HOME="/usr" \
    DEST_MAN="/usr/share/man"

  # Licenses
  #install -d "${pkgdir}"/usr/share/licenses/p7zip
  #ln -s -t "${pkgdir}"/usr/share/licenses/p7zip \
  #  /usr/share/doc/p7zip/DOCS/License.txt \
  #  /usr/share/doc/p7zip/DOCS/unRarLicense.txt

  # Remove documentation for the GUI file manager
  rm -r "${pkgdir}/usr/share/doc/p7zip/DOC/MANUAL/fm"

  install -Dm755 contrib/gzip-like_CLI_wrapper_for_7z/p7zip ${pkgdir}/usr/bin/p7zip
  install -Dm644 contrib/gzip-like_CLI_wrapper_for_7z/man1/p7zip.1 ${pkgdir}/usr/share/man/man1/p7zip.1

  chmod -R a+r,u+w,a+X "${pkgdir}/usr"
}
